package com.ldc.message.acticity

import android.graphics.Color
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.ldc.commonlibs.conf.RouteConf
import com.ldc.commonres.common.VBActivity
import com.ldc.message.adapter.MessageFragmentAdapter
import com.ldc.message.databinding.MessageActivityMessageBinding
import com.ldc.message.fragment.ReadFragment
import com.ldc.message.fragment.UnReadFragment

@Route(path = RouteConf.MessageRoute.startEnter)
class MessageActivity : VBActivity<MessageActivityMessageBinding>() {

    private var fragmentAdapter: MessageFragmentAdapter? = null
    override fun main() {
        initAdapter()
        viewBind?.also { v ->
            v.flRead.setOnClickListener { changePage(1) }
            v.flUnRead.setOnClickListener { changePage(0) }
        }

    }

    override fun createViewBinding(): MessageActivityMessageBinding =
        MessageActivityMessageBinding.inflate(layoutInflater)


    // 初始化适配器
    private fun initAdapter() {
        fragmentAdapter = MessageFragmentAdapter(
            this, mutableListOf(
                UnReadFragment.newInstance(),
                ReadFragment.newInstance()
            )
        )
        viewBind?.also { v ->
            v.ivBack.setOnClickListener { finish() }
            v.vp.adapter = fragmentAdapter
            v.vp.currentItem = 0
            v.vp.offscreenPageLimit = 2
            v.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    changePage(position)
                }
            })
        }
    }


    private fun changePage(pos: Int = 0) {
        viewBind?.also { v ->
            v.vp.currentItem = pos

            v.tvRead.textSize = if (0 == pos) 12f else 15f
            v.tvRead.setTextColor(
                if (0 == pos) resources.getColor(com.ldc.commonres.R.color.res_color_font_main) else Color.parseColor(
                    "#ff824c"
                )
            )
            v.tvUnRead.textSize = if (1 == pos) 12f else 15f
            v.tvUnRead.setTextColor(
                if (1 == pos) resources.getColor(com.ldc.commonres.R.color.res_color_font_main) else Color.parseColor(
                    "#ff824c"
                )
            )

        }
    }
}